// pages/money_tree/index.js
const app = getApp();
import trigger from '../../utils/util.js'
const AudioContext1 = wx.createInnerAudioContext('qian_1');


Page({
    /**
     * 页面的初始数据
     */
    data: {
        isShow: false,
        shaked: false,
        icons: [5, 10, 6, 7, 8, 9],
        collectNum: 0,
		shu_gan:0,
		p_name:'',
		qian:100,
		kuang_wrds:'摇手机，拿金币！',
		kuang_wrds2:'分享好友得水滴',
		get_suo:false,
		tree_lv:'lv_1',
		
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      // (trigger.trigger)(this)
		this.ren_true = true;
		var thas=this;
		this.niao_suo2=false;
		this.niao_num=0;
		this.shui_suo=false;
		this.tu_num = 1;
		
		//授权 
    (trigger.trigger)(this,'../money_tree/index','','','',options.user_id);
		
		setTimeout(function(e = thas){
			e.setData({
				kuang_on:'off',
				kuang2_on:'on'
			})
		},3000)
		
		setTimeout(function(e = thas){
			e.setData({
				kuang2_on:''
			})
		},6000);
      //树的等级
      thas.tree_get();
		//公告
		app.request(app.apiDomain + "index.php?c=mall&a=notice", {
			type:'tree'
		}, function (res) {
			thas.setData({
				gao:res.data.data
			});
		});
		
		//提交邀请人
		app.request(app.apiDomain + "index.php?c=tree&a=index",{
			invite_user:options.user_id
		}, function(res) {
			// console.log(options.user_id,'a1')
			// console.log(res,options,'邀请人')
		})
		
		// //树的等级
		// thas.tree_get();
		
		
    },
  getUserInfo: function () {
    var that = this
    if (app.getUserInfo(that)) {
      that.setData({
        user_auth: false
      })
    }
  },
	gao_off:function(){
		this.setData({
			gao_on:''
		})
	},
  return_act() {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  return_index() {
    wx.reLaunch({
      url: '../index/index',
    })
  },
	tu_bo_shu_click:function(){
		var thas=this;
		if(this.tu_num < 1){
			thas.setData({
				kuang2_on:'on',
				kuang_wrds2:'不要打扰我！',
				tu_bo_shu_on:'off'
			});
			this.tu_num++
			setTimeout(function(e = thas){
				e.setData({
					kuang2_on:'',
					tu_bo_shu_on:''
				})
			},2000);
		}else{
      thas.setData({
        tu_bo_shu_on: 'on'
      });
			// var NiaoAudio= wx.createInnerAudioContext();
			// NiaoAudio.autoplay = true;
			// NiaoAudio.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/a.mp3';
			// NiaoAudio.onPlay(() => {
			// 	console.log('开始播放')
				
			// });
			// NiaoAudio.onEnded(() => {
			// 	console.log('播放结束')
			// 	NiaoAudio.destroy()
				
			// });
      setTimeout(function () {
        thas.setData({
          tu_bo_shu_on: ''
        });
      }, 2000)
      this.tu_num = 1;
		}
	},
	title_click:function(){
		console.log(1)
		this.setData({
			gao_on:'on'
		});
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        app.SystemInfo(this)
      this.tu_bo_shu_click()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		this.lefe_right = 0;
		this.t_num = 2;
		this.n_num = 11;
		this.t_i = '0';
        var that = this;
		this.t_suo = false;
        this.setData({
            isShow: true,
            shaked: false,
        })
		//game
		wx.stopAccelerometer()
		wx.startAccelerometer({
			interval:'ui',
		});
        wx.onAccelerometerChange(function (e) {
			// console.log(e.x);
			that.setData({
				shu_gan: (e.x).toFixed(1),
			})
			if(e.x > 1){
				that.lefe_right++
				if(that.lefe_right >= that.n_num){
					that.n_num = that.n_num + 12;
					var NiaoAudio= wx.createInnerAudioContext('niao');
					NiaoAudio.autoplay = true;
					NiaoAudio.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/niao.mp3';
					NiaoAudio.onPlay(() => {
						console.log('开始播放')
					});
					NiaoAudio.onEnded(() => {
						console.log('播放结束')
						NiaoAudio.destroy()
					});
				}
			};
			if(that.t_suo){return  false}
			if(that.lefe_right >= 11){
				that.setData({
					niao_on:'on',
					kuang_wrds:'摇下全部金币吧~',
					kuang_on: that.data.get_suo ? '':that.data.kuang_on,
					get_time:that.data.get_suo ? '2小时0分':that.data.get_time,
					get_on:that.data.get_suo ? 'on':that.data.get_on,
					minute:that.data.get_suo ? 120:that.data.minute,
					get_suo:false,
				});
				setTimeout(function(e = that){
					e.setData({
						kuang_on:'off'
					})
				},3000)
				//
				that.t_suo = true;
				return  false
			}
			if(that.lefe_right == that.t_num){
				that.setData({
					p_name:that.data.p_name + 'p_' + (that.t_i == 2 ? '2 p_3':that.t_i) + ' ' 
				})
				that.t_i++;
				that.t_num = that.t_num + 4;
				//
				if(that.data.get_suo){
					app.request(app.apiDomain + "index.php?c=tree&a=yao",{}, function(res) {
						console.log(res)
						if(res.data.data.point >=0){
							that.setData({
								g:(((that.data.g/10)*10) + ((res.data.data.point/10)*10)).toFixed(1)
							})
						}
					})
					const AudioContext2 = wx.createInnerAudioContext('qian_2');
					AudioContext2.autoplay = true
					AudioContext2.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/qian.mp3'
					AudioContext2.onPlay(() => {
						console.log('开始播放')
					});
				}
				//
			};
        })
    },
	ren_click: function() {
		if (this.ren_true) {
			this.ren_true = false;
			var has = this;
			var XiongAudio= wx.createInnerAudioContext('xiong');
			XiongAudio.autoplay = true;
			XiongAudio.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/xiong.mp3';
			XiongAudio.onPlay(() => {
				console.log('开始播放')
				has.setData({
					ren_wu: 'amin'
				})
			});
			XiongAudio.onEnded(() => {
				console.log('播放结束')
				XiongAudio.destroy()
				has.setData({
					ren_wu: 'amin_2'
				})
				has.ren_true = true;
			});
		}
	},
	niao_click:function(){
		var thas=this;
		if(this.niao_suo2){return false}
		this.niao_suo2 = true;
		var Niao2= wx.createInnerAudioContext('niao');
		Niao2.autoplay = true;
		this.setData({
			niao_on:'on2',
		})
		if(this.niao_num<=1){
			this.niao_num++
			Niao2.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/niao_3.mp3';
		}else{
			this.niao_num=0;
			Niao2.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/niao_2.mp3';
		}
		Niao2.onEnded(() => {
			console.log('鸟叫结束')
			thas.niao_suo2 = false;
			this.setData({
				niao_on:'on',
			})
			Niao2.destroy()
		});
	},
	tree_get:function(){
		var thas=this;
		//树的等级
		app.request(app.apiDomain + "index.php?c=tree&a=level",{}, function(res) {
			thas.setData({
				tree_data:res.data.data,
				tree_lv:'lv_' + res.data.data.level
			})
			console.log(res.data,thas.data.tree_lv)
		})
		//金币
		app.request(app.apiDomain + "index.php?c=tree&a=index",{}, function(res) {
			// console.log(res)
			var tree = res.data;
			if(tree.status==0){
				thas.setData({
					get_time:tree.data.time,
					get_wrds:tree.info,
					get_on:'on',
					minute:tree.data.minute,
					g:tree.data.count_point,
					niao_on:'on',
				})
			}else if(tree.status==1){
				//
				thas.s_num = 3 - tree.data.count;
				var s_data = '';
				// console.log(thas.s_num)
				for(var i=0; i<thas.s_num; i++){
					s_data+='1';
					// console.log(s_data)
				}
				if(thas.data.tree_lv=='lv_3'){
					s_data=s_data+'1'
				};
				thas.setData({
					data_num:s_data,
					get_suo:true,
					g:tree.data.count_point
				})
				//
			}
		});
	},
	
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.isShow = false;
		// wx.stopGyroscope //停止
		wx.stopAccelerometer()
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // 停止监听加速度数据
        wx.stopAccelerometer()
    },
	shui_click:function(){
		if(this.data.tree_data.water<100){ return false}
		var thas=this;
		
		app.request(app.apiDomain + "index.php?c=tree&a=water",{}, function(res) {
			thas.sui_res = res.data.data;
			console.log(res)
		})
		
		var ShuiAudio= wx.createInnerAudioContext('shui');
		ShuiAudio.autoplay = true;
		ShuiAudio.src = 'http://resource.160nurse.com/canvas/healthy/img/tree/shui.mp3';
		ShuiAudio.onPlay(() => {
			console.log('开始播放')
		});
		ShuiAudio.onEnded(() => {
			console.log('播放结束')
			ShuiAudio.destroy()
		});
		this.setData({
			shui_on:'on'
		})
		setTimeout(function(e=thas){
			e.setData({
				shui_go:'go'
			})
		},1000)
		setTimeout(function(e=thas){
			e.setData({
				shui_go:'',
				tree_data:e.sui_res,
				tree_lv:'lv_' + e.sui_res.level
			})
		},3500)
		setTimeout(function(e=thas){
			e.setData({
				shui_on:''
			})
		},5000)
	},
	
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		return {
			path: "/pages/money_tree/index?user_id=" + wx.getStorageSync('user_id'),
			title: '向你推荐摇钱树，每天摇币换好礼',
			imageUrl:'http://resource.160nurse.com/canvas/healthy/img/tree/bg_1.jpg'
		}
	},
})
